package com.voe;

import com.voe.common.EchoStatistics;
import com.voe.common.OutputParam;
import com.voe.common.ReturnValue;
import com.voe.common.StatVal;

/**This sub-API supports the following functionalities:
*<br/>
*- Long-term speech and noise level metrics.<br/>
*- Long-term echo metric statistics.<br/>
*- Round Trip Time (RTT) statistics.<br/>
*- Dead-or-Alive connection summary.<br/>
*- Generation of call reports to text files.<br/>
*<br/>
*Usage example, omitting error checking:<br/>
*<br/>
*VoiceEngine voe = VoiceEngine.createInterface();<br/>
*VoEBase base = VoEBase.createInterface(voe);<br/>
*VoECallReport report = VoECallReport.createInterface(voe);<br/>
*base.init();<br/>
*ReturnValue<EchoStatistics> output = new ReturnValue<EchoStatistics>();<br/>
*report.getEchoMetricSummary(output);<br/>
*EchoStatistics stats = output.getValue();<br/>
*...<br/>
*base.terminate();<br/>
*base.release();<br/>
*report.release();<br/>
*voe.release();<br/>
*/
public class VoECallReport {

	// [start] apis
	
	public static VoECallReport createInstance(VoiceEngine voiceEngine) {

		long p = GetInterface(voiceEngine._p);
		if(p != 0)
			return new VoECallReport(p);
		return null;
	}

	public void release() {
		if (_p != 0) {
			Release(_p);
		}
	}
	
	/** Performs a combined reset of all components involved in generating<br/>
	 *  the call report for a specified |channel|. Pass in -1 to reset<br/>
	 *  all channels.
	 */
	public int resetCallReportStatistics(int channel) {
		return ResetCallReportStatistics(_p, channel);
	}

	/** Gets minimum, maximum and average levels for long-term echo metrics.
	 */
	public int getEchoMetricSummary(ReturnValue<EchoStatistics> stats) {
		OutputParam output = new OutputParam();
		int result = GetEchoMetricSummary(_p, output);
		if(result == 0) {
			EchoStatistics tmp = new EchoStatistics();
			tmp.erl = new StatVal();
			tmp.erl.min = (Integer) output.getObject()[0];
			tmp.erl.max = (Integer) output.getObject()[1];
			tmp.erl.average = (Integer) output.getObject()[2];
			tmp.erle = new StatVal();
			tmp.erle.min = (Integer) output.getObject()[3];
			tmp.erle.max = (Integer) output.getObject()[4];
			tmp.erle.average = (Integer) output.getObject()[5];
			tmp.rerl = new StatVal();
			tmp.rerl.min = (Integer) output.getObject()[6];
			tmp.rerl.max = (Integer) output.getObject()[7];
			tmp.rerl.average = (Integer) output.getObject()[8];
			tmp.a_nlp = new StatVal();
			tmp.a_nlp.min = (Integer) output.getObject()[9];
			tmp.a_nlp.max = (Integer) output.getObject()[10];
			tmp.a_nlp.average = (Integer) output.getObject()[11];
			stats.setValue(tmp);
		}
		return result;
	}

	/** Gets minimum, maximum and average levels for Round Trip Time (RTT)<br/>
	 *  measurements.
	 */
	public int getRoundTripTimeSummary(int channel, ReturnValue<Integer> delaysMs) {
		OutputParam output = new OutputParam();
		int result = GetRoundTripTimeSummary(_p, channel, output);
		if(result == 0) {
			delaysMs.setValue((Integer) output.getObject()[0]);
		}
		return result;
	}

	/** Gets the total amount of dead and alive connection detections<br/>
	 *  during a VoIP session.
	 */
	public int getDeadOrAliveSummary(int channel, 
			ReturnValue<Integer> numOfDeadDetections, 
			ReturnValue<Integer> numOfAliveDetections) {
		OutputParam output = new OutputParam();
		int result = GetDeadOrAliveSummary(_p, channel, output);
		if(result == 0) {
			numOfDeadDetections.setValue((Integer) output.getObject()[0]);
			numOfAliveDetections.setValue((Integer) output.getObject()[1]);
		}
		return result;
	}

	/** Creates a text file in ASCII format, which contains a summary<br/>
	 *  of all the statistics that can be obtained by the call report sub-API.
	 */
	public int writeReportToFile(String fileNameUTF8) {
		return WriteReportToFile(_p, fileNameUTF8);
	}

	protected VoECallReport(long p) {
		_p = p;
	}

	protected long _p;
	
	// [end]
	
	
	
	
	
	
	
	
	
	

	// [start] jni api

	// Factory for the VoECallReport sub-API. Increases an internal
	// reference counter if successful. Returns NULL if the API is not
	// supported or if construction fails.
	private static native long GetInterface(long p_voiceEngine);

	// Releases the VoECallReport sub-API and decreases an internal
	// reference counter. Returns the new reference count. This value should
	// be zero for all sub-API:s before the VoiceEngine object can be safely
	// deleted.
	private static native int Release(long p_VoECallReport);

	// Performs a combined reset of all components involved in generating
	// the call report for a specified |channel|. Pass in -1 to reset
	// all channels.
	private static native int ResetCallReportStatistics(long p_VoECallReport,
			int channel);

	// Gets minimum, maximum and average levels for long-term echo metrics.
	private static native int GetEchoMetricSummary(long p_VoECallReport,
			OutputParam stats);

	// Gets minimum, maximum and average levels for Round Trip Time (RTT)
	// measurements.
	private static native int GetRoundTripTimeSummary(long p_VoECallReport,
			int channel, OutputParam delaysMs);

	// Gets the total amount of dead and alive connection detections
	// during a VoIP session.
	private static native int GetDeadOrAliveSummary(long p_VoECallReport,
			int channel, OutputParam param);

	// Creates a text file in ASCII format, which contains a summary
	// of all the statistics that can be obtained by the call report sub-API.
	private static native int WriteReportToFile(long p_VoECallReport,
			String fileNameUTF8);

	// [end]
}
